package com.si.anddos;

import com.si.anddos.dpadapters.AnddosDbAdapter;

import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ContextMenu.ContextMenuInfo;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.Toast;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;


/**
 * A class representing Uploadload List activity in anddos application
 * 
 * @author Carlos Martinez
 * 
 */

public class UploadListActivity extends OptionsMenuActivity {
	private static final int UPLOAD_CREATE = 0;
	private static final int ACTIVITY_DETAIL = 1;
	
	private AnddosDbAdapter mDbHelper;
	private ListView listUploads;
	
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.upload_list);
		setTitle(R.string.titleUploadList);
		
		listUploads = (ListView) findViewById(R.id.listUploads);
		listUploads.setOnItemClickListener(new OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	Intent i = new Intent(parent.getContext(), UploadDetailActivity.class);
                i.putExtra(AnddosDbAdapter.colUploadID, id);
                startActivityForResult(i, ACTIVITY_DETAIL);
            }
          });
		
		// filling the list from database
        mDbHelper = new AnddosDbAdapter(this);
        
        fillData();
	}
	
	private void fillData() {
		mDbHelper.open();
        // Get all of the rows from the database and create the item list
        Cursor uploadsCursor = mDbHelper.getAllUploads();
        startManagingCursor(uploadsCursor);

        // Create an array to specify the fields we want to display in the list (only TITLE)
        String[] from = new String[]{AnddosDbAdapter.colUploadFilename, AnddosDbAdapter.colUploadFilepath, AnddosDbAdapter.colAccountUsername};

        // and an array of the fields we want to bind those fields to 
        int[] to = new int[]{R.id.textUploadRowFileName, R.id.textUploadRowFilePath, R.id.textUploadRowAccUsername};

        // Now create a simple cursor adapter and set it to display
        SimpleCursorAdapter uploads = 
            new SimpleCursorAdapter(this, R.layout.upload_list_row, uploadsCursor, from, to);
        
        // make message to user if no uploads is in database
        if(uploads.getCount()==0)
        	Toast.makeText(this, R.string.textNoUploads, Toast.LENGTH_SHORT).show();
        listUploads.setAdapter(uploads);
   
        registerForContextMenu(listUploads);
        mDbHelper.close();
    }

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		super.onCreateOptionsMenu(menu);
		// add new menu item to Options menu - other items are defined in
		// OptionsMenuActivity
		menu.add(0, Globals.NEWUPLOAD_ID, 0, R.string.menuNewUpload).setIcon(android.R.drawable.ic_menu_add);;
		return true;
	}

	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		// handling click to new account menu item
		switch (item.getItemId()) {
		case Globals.NEWUPLOAD_ID:
			createUpload();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);
	}
	
	@Override
    public void onCreateContextMenu(ContextMenu menu, View v,
            ContextMenuInfo menuInfo) {
        super.onCreateContextMenu(menu, v, menuInfo);
        // create context menu for delete account
        menu.add(0, Globals.DELETEUPLOAD_ID, 0, R.string.menuDeleteUpload);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
    	//handling click to delete account
        switch(item.getItemId()) {
            case Globals.DELETEUPLOAD_ID:
	        	AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
	        	mDbHelper.open();
	        	mDbHelper.deleteUpload(info.id);
	        	mDbHelper.close();
                fillData();
                return true;
        }
        return super.onContextItemSelected(item);
    }

	private void createUpload(){
		Intent i = new Intent(this, UploadNewActivity.class);
        startActivityForResult(i, UPLOAD_CREATE);
	}
	
	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		if(resultCode != RESULT_CANCELED)
    		fillData();
		
		super.onActivityResult(requestCode, resultCode, data);
	}
	
	
}